﻿using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NetBay.Core.Extensions;
using Rio.Framework.RioManager;
using NetBay.Core.DataAccess;

namespace Rio.Doc.DataAccess
{
    public static class DatabaseEx
    {
        private const string OracleString = "Oracle";

        /// <summary>
        /// Prepare and get new command
        /// </summary>
        /// <param name="db">Database instance</param>
        /// <param name="dbFunction">dbFunction to execute</param>
        /// <param name="AddOutputParameterWithOracle">The add output parameter with oracle.</param>
        /// <returns></returns>
        public static DbCommand GetNewCommand(this Database db, string dbFunction, bool AddOutputParameterWithOracle = true)
        {
            var l_bOutPutParameter = false;
            switch (SettingsManager.cStoreSettings.Parameter_EngineDataBase)
            {
                case OracleString:
                    l_bOutPutParameter = true;
                    break;
            }

            var dbCommand = db.GetStoredProcCommand(dbFunction);
            if (l_bOutPutParameter && AddOutputParameterWithOracle)
            {
                dbCommand.AddOracleResultParameter();
            }
            return dbCommand;
        }

        /// <summary>
        /// Executes the and get result.
        /// </summary>
        /// <param name="dbCommand">The db command.</param>
        /// <returns></returns>
        public static StoredProcedureResult ExecuteAndGetResult(this Database db, DbCommand dbCommand)
        {
            using (IDataReader reader = db.ExecuteReader(dbCommand))
            {
                while (reader.Read())
                {
                    int code = reader.GetIntValue("RESULT_CODE");
                    if (code > 0)
                    {
                        return new StoredProcedureResult(code);
                    }
                    return new StoredProcedureResult((ResultCode)code);
                }
            }
            return new StoredProcedureResult(ResultCode.ExecutedWithError);
        }

        /// <summary>
        /// Executes the and get result.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="dbCommand">The db command.</param>
        /// <param name="dbTransaction">The db transaction.</param>
        /// <returns></returns>
        public static StoredProcedureResult ExecuteAndGetResult(this Database db, DbCommand dbCommand, DbTransaction dbTransaction)
        {
            using (IDataReader reader = db.ExecuteReader(dbCommand, dbTransaction))
            {
                while (reader.Read())
                {
                    int code = reader.GetIntValue("RESULT_CODE");
                    if (code > 0)
                    {
                        return new StoredProcedureResult(code);
                    }
                    return new StoredProcedureResult((ResultCode)code);
                }
            }
            return new StoredProcedureResult(ResultCode.ExecutedWithError);
        }
    }
}